<?php
/**
 * Created by PhpStorm.
 * User: osxfal
 * Date: 2019/2/23
 * Time: 下午7:02
 */

namespace app\index\controller;

use think\Controller;

/* 消息推送管理*/
class MsgManage extends Controller{


    //判断用户是否登录
    /*public function __construct()
    {
        parent::__construct();
        if(empty(session('user')))
        {
            $this->error('对不起,您还没有登录!请先登录!','index/login');

        }

    }*/

    //消息提交  go_article
    public function msgPost(){
        $text = input('post.content');


        //     提交   日期转时间戳 存储
        $unix_time = strtotime(date('Ymd H:i:s'));

        $data = [];
        $data['content'] = $text;
        $data['posttime'] = $unix_time;
        $data['is_jpush'] = 0;
        //插入一条数据
        $res = db('article')->insert($data);

        $msgBox = [];
        if($res){
            $msgBox['result'] = 1;
            $msgBox['tips'] = '信息提交成功！';
            exit(json_encode($msgBox,320));
//            $this->success('成功提交推送信息',url('msgList'),'',2);
        }else{
            $msgBox['result'] = 2;
            $msgBox['tips'] = '信息提交失败！';
            exit(json_encode($msgBox,320));
//            $this->error('信息提交失败！');
        }
    }

    //消息发送  需要接口  jpush
    public function msgTx(){

        //接收推送按钮传递过来的 id 信息
        $id = input('post.id');
        $id = trim($id);

        $res = db('article')->find($id);


        //初始化 信息盒子
        $msgBox = [];

        if(isset($res['is_jpush']) && $res['is_jpush'] == 1){
            $msgBox['result'] = 1;
            $msgBox['tips'] = '该信息已经发送过了';
            $hi = json_encode($msgBox,320);
            exit($hi);  //前端返回提示 已经推送过了
        }
        else{
            require JPUSH . 'autoload.php';
            try{
                $client = new \JPush\Client('4b1c958c6c77448ef9e093ef','533dad1f8a779e8e753081bb');
                $client->push()
                    ->setPlatform('all')
                    ->addAllAudience()
                    ->setNotificationAlert(trim(strip_tags($res['content'])))
                    ->send();
            }catch(\Exception $e){ //推送不成功  写下日志
                $msgBox['jpush'] = '信息推送错误';
                $msgBox['err'] = '推送出错！';
                $msgBox['time'] = date('Y-m-d' ,time());
                $message = json_encode($msgBox);
                file_put_contents(LOGPATH.'jpush_logs/jpush_logs',$message);
            }

            //发送完毕后  跟新
            $where = [];
            $where['is_jpush'] = 1;
            $where['jpushtime'] = time();
            $res = db('article')->where(['id'=>$id])->update($where);
            if($res !==false){
                $msgBox['result'] = 1;
                $msgBox['tips'] = '信息发送成功！';
            }else{
                $msgBox['result'] = 2;
                $msgBox['tips'] = '信息发送成功,数据库更新失败';
            }
            $hi = json_encode($msgBox,320);
            exit($hi);
        }

    }


    /*推送列表
        表名： go_article
    */
    public function msgList(){

        //页码参数
        $page = input('get.page');
        $lines = input('get.lines');

        empty($page) && $page = 1;
        empty($lines) && $lines = 20;

        $limit = ($page - 1) * $lines . ',' . $lines;

        $res = db('article')->limit($limit)->order('id desc')->select();

        $total = db('article')->field('COUNT(id) as t')->find();
        foreach($res as $k=>&$v){

            if($v['posttime']){     //提交时间
                $v['posttime'] = date('Y-m-d H:i:s',$v['posttime']);
            }



            if($v['is_jpush'] || $v['is_jpush']!==false){//是否发送
                switch ($v['is_jpush']){
                    case 0: $v['is_jpush'] = '未推送'; break;
                    case 1: $v['is_jpush'] = '已推送'; break;
                    default:$v['is_jpush'] = '未知状态'; break;
                }

            }

            if($v['jpushtime']){//推送时间
                $v['jpushtime'] = date('Y-m-d H:i:s',$v['jpushtime']);
            }
        }

        $msg = [];
        if(!empty($res) && $total['t']!==false){
            $msg['rows'] = $res;
            $msg['total'] = $total['t'];
            $msg['result'] = 1;
            $msg['tips'] = '查询成功！';
            $msgList = json_encode($msg,320);
        }
        elseif(empty($res)){
            $msg['rows'] = [];
            $msg['total'] = 0;
            $msg['result'] = 1;
            $msg['tips'] = '查询成功！并没有匹配的数据哟~~';
            $msgList = json_encode($msg,320);
        }else{

            $msg['result'] = 2;
            $msg['tips'] = '网络忙~稍后重试';
            $msgList = json_encode($msg,320);
        }


        exit($msgList);
    }



    /*删除推送信息*/
    public function msgDelete(){
        $id = input('post.id');

        db('article')->delete($id);
        $msg['result'] = 1;
        $msg['tips'] = '信息删除成功!';
        $msgList = json_encode($msg,320);
        exit($msgList);
//        $this->success('删除成功！','msgList');
    }


}